package reflect;

import java.lang.reflect.Field;

/**
 * 暴力反射对逻辑的破坏
 */
public class ReflectDemo8 {
    public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException {
        String s1 = "abc";
        String s2 = "abc";
        System.out.println("s1:"+s1);
        System.out.println("s2:"+s2);

        Class cls = String.class;
        /*
            Field也是反射对象之一，用于表示一个属性
         */
        Field field = cls.getDeclaredField("value");
        field.setAccessible(true);
        /*
            Person p = new Person();
            p.name="王五";

            String str = "abc";
            str内部的value属性是一个char数组，内容是['a','b','c']

            //本身不能这样写，因为value属性是私有的
            str.value=new char['d','e','f'];
         */
        field.set(s1,new char[]{'d','e','f'});//s1.value=new char['d','e','f'];
        System.out.println("s1:"+s1);
        System.out.println("s2:"+s2);
        String s3 = "abc";//因为常量池的缘故，依然重用s1对象
        System.out.println("s3:"+s3);//输出def
    }
}







